@extends('admin.layouts.main_slight')
@section('content')

<style>
    body{padding: 10px;}
</style>
<?php

use App\Extend\Admin\Common\Html\EasyForm;
use App\Http\Admin\Models\Fields\System\Account;
$get = $get ?? [];

$easyForm = new EasyForm($get, Account::class);
?>
<body>

<!-- 内容主体区域 -->
<div style="padding: 15px;">
	<?php
    $url = './auth';
	?>
	<form class="layui-form layui-form-pane1" method="post" action="<?php echo $url; ?>" lay-filter="first">
        @csrf
        <div id="tree-div"></div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button type="button" class="layui-btn" lay-submit lay-filter="*">提交</button>
				<button onclick="closeIframe();" type="button" class="layui-btn">取消</button>
			</div>
		</div>
	</form>

	<br><br><br>

	<script>

		layui.use(['form','tree'], function(){
			const $ = layui.$
			const form = layui.form
            const tree = layui.tree

            // 渲染树形结构
            let inst1 = {}
            $.getJSON('./getAuthTreeData?account_id=<?php echo $account['id'] ?>', function(d) {
                if(d.code !== 0) {
                    layer.msg(d.msg)
                    return false
                }

                const data = d.data
                inst1 = tree.render({
                    elem: '#tree-div'  //绑定元素
                    ,showCheckbox: true
                    ,data: data
                });
            })



			//自定义验证规则
			form.verify({

			});

			//监听提交
			form.on('submit(*)', function(data) {
                //获得选中的节点
                var checkData = inst1.getChecked('tree-div');
                ids = getIdListByTree(checkData)

                data.field.id = <?php echo $account['id'] ?>;
                data.field.ids = ids;

                $.post('<?php echo $url ?>' , data.field , function(d) {
					if(d.code != 0) {
						//返回失败
						layer.msg(d.msg);
						return false;
					}
					layer.msg('成功');
					setTimeout(function() {
						const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						parent.layer.close(index); //再执行关闭
						parent.layui.table.reload('list');
					},1000);
				});
				return false;
			});

		});

        function getIdListByTree(checkData) {
            const ids = [];
            for(let i=0; i<checkData.length; i++) {
                ids.push(checkData[i]['id'])
                const menuList = checkData[i].children;

                for(let j=0; j<menuList.length; j++) {
                    ids.push(menuList[j]['id'])
                }
            }
            return ids
        }

	</script>
</div>

</body>

@endsection